<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/1
 * Time: 14:44
 */
namespace Admin\Controller;

class CustomerController extends AdminController
{
    protected $searchRule = array(
        'customer_no' => array('/^\d{18}$/','eq'),
        'id_no' => array('/^\d{18}$/','eq'),
        'tel' => array('/^\d{11}$/','eq'),
        'attestation_tel' => array('/^\d{11}$/','eq'),
        'contact' => array('/^\d{11}$/','like','%','%'),
        'name' => array('/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u','like','%','%')
    );

    public function read(){
        $param = $this->getParam();
        if($param){
            $modelName = $this->modelName;
            $pk = D($modelName)->getPk();
            $field = array();
            $relation = array('families');
            if($param[$pk]){
                $data = D($modelName)->field($field)->relation($relation)->find($param[$pk]);
            }else{
                $data = D($modelName)->where($param)->field($field)->relation($relation)->find();
            }
            if($data){
                $res['stat'] = 1;
                $res['data'] = $data;
                $res['msg'] = '查找到1条数据。';
            }elseif($data == null){
                $res['stat'] = 0;
                $res['data'] = $data;
                $res['msg'] = '查找到0条数据。';
            }else{
                $res['stat'] = 0;
                $res['data'] = $data;
                $res['msg'] = '查找失败！';
            }
        }else{
            $res['stat'] = 0;
            $res['msg'] = '请提交参数！';
        }
        $this->ajaxReturn($res);
    }

    public function search(){
        $param = $this->getParam();
        $modelName = $this->modelName;
        $scope = array(
            'order' => 'customer_no'
        );
        $relation = array('families','family_member');
        $search = $this->getSearch();
        $search = array_merge($search,array('deleted'=>0));
        $page = $this->getPage();
//        myDump($search);
//        myDump($param);
        if($id = I('get.sessionId')){
            $id = getCommunityId($id);
//            var_dump($id);
            $search = $this->getSearch('a.');
            $search = array_merge($search,array('a.deleted'=>array('eq',0)));
//            var_dump($search);
//            $data= D('Customer')->alias('a')->field('a.*')->join("LEFT JOIN zh_family_member b on a.id=b.customer_id LEFT JOIN zh_family c on b.family_id=c.id")->where("c.community_id IN ($id)")->page($page[0],$page[1])->select();
            $data= D('Customer')->alias('a')->field('a.*')->join("LEFT JOIN zh_family_member b on a.id=b.customer_id LEFT JOIN zh_family c on b.family_id=c.id")->where("c.community_id IN ($id)")->where($search)->scope($scope)->relation($relation)->page($page[0],$page[1])->select();
//            var_dump($data);
            if($data){
                foreach ($data as $k => &$v){
                    $v['familyId'] = $v['families']['0']['id'];
                }
                $res['stat'] = 1;
                $res['data'] = $data;
                $res['count'] = D('Customer')->alias('a')->field('a.*')->join("LEFT JOIN zh_family_member b on a.id=b.customer_id LEFT JOIN zh_family c on b.family_id=c.id")->where("c.community_id IN ($id)")->where($search)->scope($scope)->count();
                $res['msg'] = '查找到1条数据。';
            }elseif($data == null){
                $res['stat'] = 1;
                $res['data'] = array();
                $res['count'] = 0;
                $res['msg'] = '查找到0条数据。';
            }else{
                $res['stat'] = 0;
                $res['data'] = $data;
                $res['msg'] = '查找失败！';
            }
        }else{
//            myDump($param);
//            myDump($search);
            $data = D($modelName)->where($param)->where($search)->scope($scope)->relation($relation)->page($page[0],$page[1])->select();
            if($data){
                foreach ($data as $k => &$v){
                    $v['familyId'] = $v['families']['0']['id'];
                }
                $res['stat'] = 1;
                $res['data'] = $data;
                $res['count'] = $data = D($modelName)->where($param)->where($search)->scope($scope)->relation($relation)->count();
                $res['msg'] = '查找到1条数据。';
            }elseif($data == null){
                $res['stat'] = 1;
                $res['data'] = array();
                $res['count'] = 0;
                $res['msg'] = '查找到0条数据。';
            }else{
                $res['stat'] = 0;
                $res['data'] = $data;
                $res['msg'] = '查找失败！';
            }
        }
//        myDump(D($modelName)->getLastSql());
        $res['recordsTotal'] = $res['count'];
        $res['recordsFiltered'] = $res['count'];
        $this->ajaxReturn($res);
    }

    public function add(){
        $req = $this->getReq();
        $param = $this->getParam();
        $param['name'] = $param['name'] ? $param['name'] : '临时客户';
        $param['sex'] = $param['sex'] ? $param['sex'] : 'M';
        if($param['tel'])
            $param['tel'] = preg_replace('/(0*)(1[^0])(\d{9})/','$2$3',$param['tel']);
        if($param['phone'])
            $param['phone'] = preg_replace('/(0*)(1[^0])(\d{9})/','$2$3',$param['phone']);
        if($param){
            $modelName = $this->modelName;
            $field = array();
            $relation = array();
            $create = $field ? D($modelName)->field($field)->create($param,1) : D($modelName)->create($param,1);
            if($create){
                $data = D($modelName)->relation($relation)->add();
                if($data){
                    if($req['family_id']){
                        $data['family_member'] = D('FamilyMember')->data(array('family_id'=>$req['family_id'],'customer_id'=>$data))->add();
                    }
                    $pk = D($modelName)->getPk();
                    $res['stat'] = 1;
                    if($pk){
                        $res['pk'] = $data;
                    }
                    $res['data'] = $data;
                    $res['msg'] = '新增数据1条。';
                }else{
                    $res['stat'] = 0;
                    $res['msg'] = '新增数据失败！';
                }
            }else{
                $res['stat'] = 0;
                $res['msg'] = D($modelName)->getError();
            }
        }else{
            $res['stat'] = 0;
            $res['msg'] = '请提交参数！';
        }
        $this->ajaxReturn($res);
    }

    public function status(){
        $req = $this->getReq();
        if(strpos($req['id'],',')){
            $where['id'] = array('in',explode(',',$req['id']));
        }else{
            $where['id'] = $req['id'];
        }
        $param['status'] = $req['status'];
        if($req['id'] && isset($req['status']) && $req['status']!=''){
            $modelName = $this->modelName;
//            $pk = D($modelName)->getPk();
//            $field = array();
//            $relation = array();
            $data = D($modelName)->where($where)->save($param);
            if($data){
                $res['stat'] = 1;
                $res['data'] = $data;
                $res['msg'] = '更新数据成功。';
            }else{
                $res['stat'] = 0;
                $res['msg'] = '更新数据失败！';
            }
        }else{
            $res['stat'] = 0;
            $res['msg'] = '请提交参数！';
        }
        $this->ajaxReturn($res);
    }

    public function addAll(){
//        $data = D('OldCrm/OrdersCustomer')->where(array('username'=>'冯鹏钰'))->relation(true)->find();

        if(!S('customer')){
            $data = D('OldCrm/OrdersCustomer')->query('select customer.*,phone.number from orders_customer as customer,orders_phone as phone where customer.id = phone.owner_id');
            S('customer',$data);
        }
//        dump(S('customer'));exit;
        $customers = S('customer');
//        M('customer')->startTrans();
        foreach($customers as $k=>$v){
//            echo date('YmdHis',strtotime($v['create_at']));exit;
            $customer = array();
            $customer['name'] = $v['username'];
            $customer['sex'] = $v['sex'] == 'W' ? 'F' : 'M';
            $customer['create_time'] = $v['create_at'];
            $customer['tel'] = $v['number'];
            $customer['customer_no'] = generateCustomerNo($v['create_at']);
            while(M('Customer')->where(array('customer_no'=>$customer['customer_no']))->find()){
                $customer['customer_no'] = generateCustomerNo($v['create_at']);
            }
            M('Customer')->data($customer)->add();
            if($k%5000 == 0 && $k != 0){
                sleep(1);
            }
//            if(!M('customer')->data($customer)->add()){
//                echo 'a';
//                M('customer')->rollback();
//            }
        }
//        M('customer')->commit();
//        dump($data);
    }

    public function delete()
    {
        $id = I('id');
        $where['id'] = array('in',$id);
        $data['deleted'] = 1;
        D('Customer')->where($where)->delete();
        $res['stat'] = 1;
        $this->ajaxReturn($res);
    }
}